<?php

namespace Common\Model;

use Think\Model;

class BbsReplyModel extends Model
{
    protected $_validate = array(
        array('uid', 'require', 'uid必须填写'),
        array('bid', 'require', 'bid必须填写'),
        array('content', 'require', '内容必须填写'),
    );

    protected $_auto = array(
        array('praise', '0'),
        array('click', '1'),
        array('addtime', 'time', 1, 'function')
    );

    /**
     * 获取列表
     * @param array  $map
     * @param bool   $page
     * @param string $sort
     * @param int    $num
     * @return array
     */
    public function get_list($map = array(), $page = false, $sort = '', $num = 10,$startrow=0)
    {
        $limit = $num ? $num : 10;

        if (empty($sort)) {
            $sort = 'id DESC';
        }
        if ($page) {
            $count = $this->where($map)->count();
            $Page = pager($count, $limit);
            $limit = $Page->firstRow . ',' . $Page->listRows;
            $page_str = $Page->fshow();
        }

        $list = $this->where($map)->order($sort)->limit($startrow.','.$limit)->select();
        $list = array_map(array($this, 'fill'), $list);

        $return['list'] = $list;
        $return['count'] = $page ? $count : count($list);
        $return['page'] = $page_str;

        return $return;
    }

    /**
     * 获取一条记录
     */
    public function get_reply_one($id)
    {
        return $this->find($id);
    }

    /**
     * 属性填充
     * @param array $item
     * @return array
     */
    public function fill($item)
    {
        # 需要扩展的属性
        $item['addtime_str'] = daterange($item['addtime']);
		$item['content_cut'] = cut_str($item['content'], 100);
        return $item;
    }

    /**
     * 添加主题回复
     * @param $data
     * @param $bid
     * @param $uid
     * @return mixed
     */
    public function reply_add($data, $bid, $uid)
    {
        if (!$this->create($data)) {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
            return $ret;
        } else {
            $this->bid = $bid;
            $this->uid = $uid;
            $maxfloor = $this->where(array('bid' => $bid))->max('floor');
            $this->floor = $maxfloor + 1;
        }

        $newid = $this->add();
        if (!$newid) {
            $ret['status'] = false;
            $ret['msg'] = '保存失败';
            return $ret;
        } else {
            # 添加主题回复相关操作
            // bbs表最后回复人/最后回复时间/回复数统计
            $count = M('BbsReply')->where(compact('bid'))->count();
            $bbsdata['replycount'] = $count;
            $bbsdata['replyuid'] = $uid;
            $bbsdata['replytime'] = time();
            $map['id'] = $bid;
            M('Bbs')->where($map)->save($bbsdata);
            // 记录排行数据 用于计算answer(助人)的排行
            D('Rank')->add_record('answer', $uid, 1);
            D('WeixinMsgtpl')->send_userreplymsg($uid, $bid, $data['content'], 'bbs');  // 回复微信模板消息通知
            D('Task')->exe_reply_question($uid);//完成回答问题任务

            $ret['status'] = true;
            $ret['msg'] = '保存成功';
            return $ret;
        }
    }

    /**
     * 回帖点赞
     */
    public function praise($uid, $rid)
    {
        $map['uid'] = $uid;
        $map['rid'] = $rid;
        $praise = M('BbsReplyPraise')->where($map)->find();
        if ($praise) {
            $res['status'] = 2;
            $res['msg'] = '已点过赞';
            return $res;
        }
        $ret = D('BbsReplyPraise')->praise_add($uid, $rid);
        // 点赞失败
        if (!$ret['status']) {
            $res['status'] = 0;
            $res['msg'] = $ret['msg'];
            return $res;
        }

        # 点赞成功相关操作
        // 更新主题的点赞数
        $this->where(array('id' => $rid))->setInc('praise');
        $bbsreply = D('BbsReply')->find($rid);
        D('Rank')->add_record('praise', $bbsreply['uid'], 1);  // 记录排行数据

        $res['status'] = 1;
        $res['msg'] = $ret['msg'];
        $q = $this->field('praise')->find($rid);
        $res['count_praise'] = $q['praise'];
        return $res;
    }
	 /**
     * 删除回帖
     * @param array|int $ids
     * @return int
     */
    public function del_reply($ids,$uid='')
    {
        $ids = is_array($ids) ? implode(",", $ids) : $ids;

        if (empty($ids)) {
            $ret['status']  = false;
            $ret['content'] = 'ID不能为空';
            return $ret;
        }
        if (!is_match_ids($ids)) {
            $ret['status']  = false;
            $ret['content'] = '参数错误';
            return $ret;
        }

        $count = 0;
        $list  = $this->select($ids);
		if (!empty($uid)){
			$map['uid']=intval($uid);
			}
		$map['id']=array('IN',"{$ids}");
        $count = $this->where($map)->delete();
		# 这里加入删除后的数据统计

        $ret['status']  = true;
        $ret['content'] = '删除成功';
        $ret['count']   = (int)$count;

        return $ret;
    }

    
}

?>